<!-- 年度计划 弹窗 -->
<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="showAnnualPlanDialog"
      :before-close="cancel"
      width="628px"
      append-to-body
      custom-class="dialog-box"
    >
      <el-form
        ref="queryParams"
        :model="queryParams"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="计划名称" prop="planId">
          <el-select v-model="queryParams.planId" placeholder="请选择" style="width:100%">
            <el-option
              v-for="dict in planNameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addPreliminaryInfo} from "@/api/projectCompany/myProject";
import { getAnnualist, supplementToYear } from "@/api/manage/annualPlan";
export default {
  name: "AnnualPlanDialog",
  props: {
    showAnnualPlanDialog: {
      type: Boolean,
      default: false,
    },
    proId: {
      type: String,
      default: "",
    },
    rowData: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["reload"],
  data() {
    return {
      // 弹出层标题
      title: "年度计划",
      // 表单参数
      queryParams: {},
      // 表单校验
      rules: {
        planId: [
          { required: true, message: "请选择项目", trigger: "change" },
        ],
      },
      // 计划名称字典查询
      planNameOptions: [{ dictValue: "0", dictLabel: "未对接口，提交无效" }],
    };
  },
  watch: {
    showAnnualPlanDialog(val) {
      if(val==true && this.proId){
          this.getAnnualOptions()
      }
    },
  },
  methods: {
    // 重置表单
    reset() {
      this.queryParams = {
        planId: undefined,
      };
      this.resetForm("queryParams");
    },
    /** 取消按钮 */
    cancel() {
      this.reset()
      this.$emit("changeShowDialog", "false"); //调用父组件的自定义事件，并传值
    },
    /** 确定按钮 */
    submitForm() {
      this.$refs["queryParams"].validate((valid) => {
        if (valid) {
          supplementToYear(this.queryParams.planId,this.proId).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.$emit("changeShowDialog", "false");
              this.reset()
              this.reload()
            }
          })
        }
      });
    },
    getAnnualOptions() {
      getAnnualist({
        isState: 46,
        pageSize: 1000,
      }).then(res => {
        if (res.code === 200) {
          this.planNameOptions = res.rows.map(item => {
            return {
              dictValue: item.planId,
              dictLabel: item.name,
            }
          })
        }
      })
    }
  },
};
</script>

<style lang="scss" scoped></style>
